package com.esbi.projectwebsocket.aiRequest;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class AiRequestService {

    private static final Map<Integer, String> conversationIdsByHour = new HashMap<>();
    private static int currentHour = -1;



    public String processInput(String input) throws JSONException {
    String url = "http://112.111.7.68:13201/ele_bill";
    HttpHeaders headers = new HttpHeaders();
    headers.set("Content-Type", "application/json");
    headers.set("Accept", "text/event-stream");
    headers.set("Authorization", "Basic YmFja2VuZF9hY2NvdW50OmJhY2tlbmRfMjAyNDA1MzA=");
    //String requestBody = "{\"conversation_id\":\"1001012\",\"input\":\"上海中心大厦\",\"doc_content\":\"\"}";

    // 创建一个JSONObject来存储键值对
    JSONObject jsonObject = new JSONObject();
    // 向JSONObject中添加动态数据
    jsonObject.put("conversation_id","1001012");
    jsonObject.put("input", input);
    // 假设doc_content总是空的，这里直接设为""
    jsonObject.put("doc_content", "");
    String requestBody = jsonObject.toString();

    //System.out.print("\33[41;1m"+jsonObject+"\n\33[0m");//红色背景
    //System.out.print("\33[42;1m"+requestBody+"\n\33[0m");//红色背景
    HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
    //System.out.println(response.getBody());//注释掉应该打印在控制台的信息
    String body = response.getBody();
    //不对数据做修改直接返回原始数据。
    //String returnBody = extractAndCombineTexts(body);
    //System.out.print("\33[41;1m"+returnBody+"\n\33[0m");//红色背景
    System.out.println("request:"+input);
    return body ;
    }


    public static String getConversationId() {
        int nowHour = ZonedDateTime.now(ZoneId.systemDefault()).getHour();
        if (nowHour!= currentHour) {
            currentHour = nowHour;
            Random random = new Random();
            int randomNumber = random.nextInt(9000000) + 1000000;
            conversationIdsByHour.put(currentHour, String.valueOf(randomNumber));
        }
        return conversationIdsByHour.get(currentHour);
    }

    public static String extractAndCombineTexts(String input) {
        Pattern pattern = Pattern.compile("\"text\": \"([^\"]+)\"");
        Matcher matcher = pattern.matcher(input);
        StringBuilder combinedText = new StringBuilder();
        while (matcher.find()) {
            String text = matcher.group(1);
            if (!text.contains("\n") &&!text.contains("\\n")) {
                combinedText.append(text);
            }
        }
        return combinedText.toString();
    }



}